//
//  TWTabBarController.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/3/9.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit

class TWTabBarController: UITabBarController {

    // MARK:- 懒加载
    fileprivate lazy var choicenessViewController : TWChoicenessViewController = TWChoicenessViewController()
    fileprivate lazy var discoverViewController : TWDiscoverViewController = TWDiscoverViewController()
    fileprivate lazy var subscriptionViewController : TWSubscriptionViewController = TWSubscriptionViewController()
    fileprivate lazy var mineViewController : TWMineViewController = TWMineViewController()

    var previousClickedTabBarButtonTag : Int = 0
    
    // MARK:- 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        addAllChildViewControllers()
        delegate = self
    }
}


// MARK:- 添加子控制器
extension TWTabBarController {
    /// 添加四个控制器
    fileprivate func addAllChildViewControllers() {
        addOneViewController(viewController: choicenessViewController, needNavigation: true,imageName: "ic_tab_1_49x49_", selectedImageName: "ic_tab_1_selected_49x49_", tag: 0)
        addOneViewController(viewController: discoverViewController, needNavigation: true,imageName: "ic_tab_2_49x49_", selectedImageName: "ic_tab_2_selected_49x49_", tag: 1)
        addOneViewController(viewController: subscriptionViewController, needNavigation: true,imageName: "ic_tab_3_49x49_", selectedImageName: "ic_tab_3_selected_49x49_", tag: 2)
        addOneViewController(viewController: mineViewController, needNavigation: false,imageName: "ic_tab_4_49x49_", selectedImageName: "ic_tab_4_selected_49x49_", tag: 3)
    }
    /// 添加一个控制器
    private func addOneViewController(viewController : UIViewController, needNavigation : Bool, imageName : String, selectedImageName : String, tag : Int) {
        let vc = viewController
        vc.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
        vc.tabBarItem.image = imageOriginalWithName(imageName: imageName)
        vc.tabBarItem.selectedImage = imageOriginalWithName(imageName: selectedImageName)
        vc.tabBarItem.tag = tag
        if needNavigation {
            let vcNav = TWNavigationViewController(rootViewController: vc)
            addChildViewController(vcNav)
        } else {
            addChildViewController(vc)
        }
    }
    /// 使用原始图片
    private func imageOriginalWithName(imageName : String) -> UIImage {
        var image = UIImage(named: imageName)
        image = image?.withRenderingMode(.alwaysOriginal)
        return image!
    }
}

extension TWTabBarController : UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if viewController.tabBarItem.tag == previousClickedTabBarButtonTag {
            /// 发出通知
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: TabBarButtonDidRepeatClickNotification), object: nil)
        }
        // 点击完之后将这次点击的按钮赋值给上次点击按钮保存起来
        previousClickedTabBarButtonTag = viewController.tabBarItem.tag
    }
}
